﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer

Public Class NextAppointmentControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            'If AppointmentTime.Value Is Nothing Then
            '    CommonMessages.ShowValidationMsg("Please select time!")
            '    Exit Function
            'End If

            If String.IsNullOrEmpty(PatientControl.SelectedItem.DisplayName) Then
                CommonMessages.ShowValidationMsg("Please select patient!")
                Exit Function
            End If

            If String.IsNullOrEmpty(AssignedDrComboBox.Text) Then
                CommonMessages.ShowValidationMsg("Please select doctor!")
                Exit Function
            End If

            If AppointDateDatePicker.Value < Date.Now Then
                CommonMessages.ShowValidationMsg("Selected date should not be less than current date!")
                Exit Function
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New AppointmentDataLayer()
        Dim app As New PatientAppointment
        app.patientId = PatientControl.SelectedItem.Id
        app.consultingDoc = AssignedDrComboBox.SelectedValue
        app.appDataTime = AppointDateDatePicker.Value ' String.Format(AppointmentTime.Value, "hh:mm")
        app.appointmentType = AppointmentTypeComboBox.SelectedValue
        app.appConfirm = ConfirmRadioButton.IsChecked
        app.appTentative = TentativeRadioButton.IsChecked
        If dataProvider.SaveAppointment(app, ActionMode) = True Then
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode

    Private Sub ClientEntryUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        PatientControl.LoadData()
        AddNew()
    End Sub

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        'AppointmentTime.Value = Date.Now
        AppointmentCalendar.SelectedDate = Date.Now
        AppointDateDatePicker.Value = Date.Now
        PatientControl.LoadData(0)
        ActionMode = FormAction.ADD
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)

        lst = dataProvider.GetLookupList(LookupTypes.AppointmentType)
        AppointmentTypeComboBox.ItemsSource = lst

        'lst = dataProvider.GetLookupList(LookupTypes.AppointmentStatus)
        'AppointmentStatusComboBox.ItemsSource = lst

        lst = dataProvider.GetDoctorsLookList()
        AssignedDrComboBox.ItemsSource = lst

        'Dim dataProvider1 As New PatientDataLayer()
        'Dim lstCl As List(Of Lookups)
        'lstCl = dataProvider1.GetPatientNameIDList()
        'PatientNameComboBox.ItemsSource = lstCl
    End Sub

    Public Sub FindData() Implements IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        'FindStackPanel.Visibility = Visibility.Visible
    
    End Sub

    Public Sub BackView() Implements IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        'FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub AppointmentCalendar_SelectedDatesChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles AppointmentCalendar.SelectedDatesChanged
        FillPatientAppointment()
    End Sub

    Private Sub AssignedDrComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles AssignedDrComboBox.SelectionChanged
        FillPatientAppointment()
    End Sub

    Private Sub FillPatientAppointment()
        If AssignedDrComboBox.SelectedItem Is Nothing Then Exit Sub
        Dim lst As List(Of PatientAppointment)
        Dim dataProvider As New AppointmentDataLayer()
        Dim startDate As Date
        Dim endDate As Date

        startDate = AppointmentCalendar.SelectedDates(0)
        endDate = startDate
        If AppointmentCalendar.SelectedDates.Count > 1 Then
            endDate = AppointmentCalendar.SelectedDates(AppointmentCalendar.SelectedDates.Count - 1)
        Else
        End If
        lst = dataProvider.GetAppointments(AssignedDrComboBox.SelectedValue, startDate, endDate)
        AppointmentDataGrid.ItemsSource = lst
    End Sub

    Private PatientID As Long
    Public Sub New(ByVal _PatientID As Long)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.PatientID = _PatientID
    End Sub
End Class
